package com.edu.controller;

import com.edu.bean.User;
import com.edu.mapper.HobbyMapper;
import com.edu.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
public class UserController {

    @Resource
    private UserService userService;

    private  ModelAndView mv = new ModelAndView();

    @RequestMapping("/login")
    public Object login(User user, HttpSession session){
        Map<String, Object> map = userService.login(user);
        if (map.get("logUser") != null) {
            mv.addObject("logUser",map.get("logUser"));
            session.setAttribute("logUser",map.get("logUser"));
            mv.setViewName("session.jsp");
        } else {
            mv.addObject("msg",map.get("msg"));
            mv.setViewName("login.jsp");
        }
        return mv;
    }

    @RequestMapping("/register")
    public Object register(User user,int[] hobbyType){
        System.out.println(user);
        for (int s : hobbyType) {
            System.out.println(s);
        }
        Map<String,Object> userMap = userService.registerUser(user,hobbyType);
        if (userMap.get("registerOk") != null) {
            mv.addObject("msg","注册成功");
        } else {
            mv.addObject("msg",userMap.get("msg"));
        }
        mv.setViewName("login.jsp");
        return mv;
    }
}
